package com.smile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smile.api.AirportApi;
import com.smile.api.OrganizationApi;
import com.smile.mapper.InspectionTaskMapper;
import com.smile.mapper.WayPointMapper;
import com.smile.mapper.WayPointTaskInfoMapper;
import com.smile.model.domain.FeisuanInspectionTask;
import com.smile.model.domain.FeisuanWayPointTaskInfo;
import com.smile.model.dto.request.GetInspectionTaskByFlightIdRequestDTO;
import com.smile.model.dto.request.GetWayPointTaskInfoByFlightIdRequestDTO;
import com.smile.model.dto.request.WayPointTaskInfoRequestDTO;
import com.smile.model.response.response.GetInspectionTaskByFlightIdResponseDTO;
import com.smile.model.response.response.WayPointTaskInfoDTO;
import com.smile.service.InspectionTaskService;
import com.smile.service.WayPointTaskInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WayPointTaskInfoServiceImpl implements WayPointTaskInfoService {

    @Autowired
    private WayPointTaskInfoMapper wayPointTaskInfoMapper;

    @Autowired
    private AirportApi airportApi;

    @Autowired
    private OrganizationApi organizationApi;
    @Override
    public WayPointTaskInfoDTO getWayPointTaskInfo(GetWayPointTaskInfoByFlightIdRequestDTO getWayPointTaskInfoByFlightIdRequestDTO){
        WayPointTaskInfoDTO wayPointTaskInfoDTO = new WayPointTaskInfoDTO();
        QueryWrapper<FeisuanWayPointTaskInfo> queryWrapper = new QueryWrapper<>();
        if(getWayPointTaskInfoByFlightIdRequestDTO.getFlightId() != null){
            queryWrapper.eq("flight_id",getWayPointTaskInfoByFlightIdRequestDTO.getFlightId());
        }

        if (getWayPointTaskInfoByFlightIdRequestDTO.getType() != null){
            queryWrapper.eq("type",getWayPointTaskInfoByFlightIdRequestDTO.getType());
        }

        if (getWayPointTaskInfoByFlightIdRequestDTO.getIndex() != null){
            queryWrapper.eq("`index`",getWayPointTaskInfoByFlightIdRequestDTO.getIndex());
        }
        queryWrapper.last("limit 1");
        FeisuanWayPointTaskInfo feisuanWayPointTaskInfo = wayPointTaskInfoMapper.selectOne(queryWrapper);
        if (feisuanWayPointTaskInfo != null){
            wayPointTaskInfoDTO = BeanUtil.copyProperties(feisuanWayPointTaskInfo, WayPointTaskInfoDTO.class);
        }
        return  wayPointTaskInfoDTO;
    }

    @Override
    public Integer addWayPointTaskInfo(WayPointTaskInfoRequestDTO wayPointTaskInfoRequestDTO){
        FeisuanWayPointTaskInfo feisuanWayPointTaskInfo = BeanUtil.copyProperties(wayPointTaskInfoRequestDTO,FeisuanWayPointTaskInfo.class);
        Integer a = wayPointTaskInfoMapper.insert(feisuanWayPointTaskInfo);
        return   a;
    }

//    @Override
//    public GetInspectionTaskByFlightIdResponseDTO getInspectionTaskByFlightId(GetInspectionTaskByFlightIdRequestDTO getInspectionTaskByFlightIdRequestDTO) {
//        String flightId = getInspectionTaskByFlightIdRequestDTO.getFlightId();
//        QueryWrapper<FeisuanInspectionTask> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("flight_id", flightId);
//        FeisuanInspectionTask feisuanInspectionTasks = inspectionTaskMapper.selectOne(queryWrapper);
//        GetInspectionTaskByFlightIdResponseDTO getInspectionTaskByFlightIdResponseDTO = new GetInspectionTaskByFlightIdResponseDTO();
//        getInspectionTaskByFlightIdResponseDTO.setFlightId(flightId);
//        getInspectionTaskByFlightIdResponseDTO.setAirportId(feisuanInspectionTasks.getAirlineId());
//        return getInspectionTaskByFlightIdResponseDTO;
//    }
//
//    @Override
//    public void editInspectionTaskByFlightId(String flightId,Integer errorCode,Integer status,Integer currentWaypointIndex){
//        QueryWrapper<FeisuanInspectionTask> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("flight_id", flightId);
//        FeisuanInspectionTask feisuanInspectionTasks = inspectionTaskMapper.selectOne(queryWrapper);
//        if(feisuanInspectionTasks!=null){
//            feisuanInspectionTasks.setErrorCode(errorCode);
//            feisuanInspectionTasks.setStatus(status);
//            if (currentWaypointIndex != null){
//                feisuanInspectionTasks.setCurrentWaypointIndex(currentWaypointIndex);
//            }
//            Integer res = inspectionTaskMapper.updateById(feisuanInspectionTasks);
//        }
//
//    }
}
